package com.liuya.db;

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

public class DataSourceProviderJndiImpl extends DataSource  implements java.io.Serializable {
	private String name;
	private javax.sql.DataSource dataSource;
	private Properties prop;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public javax.sql.DataSource getDataSource() {
		return dataSource;
	}

	public void setup(Properties prop) {
		this.prop = prop;
		String jndiName = prop.getProperty("jndiName");
		try {
			InitialContext ctx = new InitialContext();
			dataSource = (javax.sql.DataSource) ctx.lookup(jndiName);
		} catch (NamingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public String getValidInfo(Properties prop) {
		this.prop = prop;
		if (prop.containsKey("jndiName")) {
			return null;
		}
		return "Required properties: jndiName";
	}

	public String toString() {
		return "PoweredBy Jndi [ jndiName=" + prop.getProperty("jndiName") + "]";
	}
}
